package com.demo.util;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


public class LoggerUtils
{
	
	public static void main(String[] args)
	{
		LoggerUtils.debug("afaf");
		LoggerUtils.error("bbb");
		System.out.println(logger.isDebugEnabled());;
	}
	
	private static Logger logger = Logger.getLogger(LoggerUtils.class);

	/**
	 * 构造方法，初始化Log4j的日志对象
	 */
	private LoggerUtils() {	}

	/**
	 * 获取构造器，根据类初始化Logger对象
	 * 
	 * @param Class
	 *            Class对象
	 * @return Logger对象
	 */
	public static Logger getLogger(Class classObject) {
		return Logger.getLogger(classObject);
	}

	/**
	 * 获取构造器，根据类名初始化Logger对象
	 * 
	 * @param String
	 *            类名字符串
	 * @return Logger对象
	 */
	public static Logger getLogger(String loggerName) {
		return Logger.getLogger(loggerName);
	}

	public static  void debug(Object object) {
		if(!logger.isDebugEnabled())return;
		logger.debug("------->>>" + Thread.currentThread().getStackTrace()[2].toString() + " : " + object);
	}
	

	public static  void debug(Object object, Throwable e) {
		if(!logger.isDebugEnabled())return;
		logger.debug(Thread.currentThread().getStackTrace()[2].toString() + " : " + object, e);
	}

	public static  void info(Object object) {
		logger.info(Thread.currentThread().getStackTrace()[2].toString() + " : " + object);
	}

	public static void info(Object object, Throwable e) {
		logger.info(Thread.currentThread().getStackTrace()[2].toString() + " : " + object, e);
	}

	public  static void warn(Object object) {
		logger.warn(Thread.currentThread().getStackTrace()[2].toString() + " : " + object);
	}

	public  static void warn(Object object, Throwable e) {
		logger.warn(Thread.currentThread().getStackTrace()[2].toString() + " : " + object, e);
	}
	

	public  static void error(Object object) {
		logger.error(Thread.currentThread().getStackTrace()[2].toString() + " : " + object);
	}
	
	public  static void errorCode(Object object) {
		logger.error(Thread.currentThread().getStackTrace()[2].toString() + "--code: " + object);
	}

	/***
	 * 直接打印异常栈贞信息
	 * @param object
	 * @param e
	 */
	public  static void error1(Object object, Throwable e) {
		logger.error(Thread.currentThread().getStackTrace()[2].toString() + " : " + object, e);
	}
	
	
	/**
	 * 输出url,params,ip,exception position,msg,异常栈贞信息
	 * @param object
	 * @param e
	 */
	public  static void error(Object object, Throwable e) 
	{
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if(requestAttributes == null)
		{
			logger.error(object , e);
			return ;
		}
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
		HttpServletRequest request = servletRequestAttributes.getRequest();
		if(request == null)
		{
			logger.error(object , e);
			return ;
		}
		
		Map<String , String[]> map = request.getParameterMap();
		Iterator<Entry<String , String[]>> iterator = map.entrySet().iterator();
		StringBuilder str = new StringBuilder();
		while(iterator.hasNext())
		{
			Entry<String , String[]> entry = iterator.next();
			str.append(entry.getKey());
			str.append(Arrays.toString(entry.getValue()));
		}
		String path = ((HttpServletRequest) request).getRequestURI();
		String ip = RequestUtil.getIpAddr(request);
		
		StringBuilder builder = new StringBuilder();
		builder.append("\n\r\n\r");
		builder.append("----------------------start-----------------");
		builder.append("\nurl               : ");
		builder.append(path);
		builder.append("\nip                : ");
		builder.append(ip);
		builder.append("\nparams            : ");
		builder.append(str);
		builder.append("\nexception position: ");
		builder.append(Thread.currentThread().getStackTrace()[2].toString());
		builder.append("\nmsg               : ");
		builder.append(Thread.currentThread().getStackTrace()[2].toString() + " : " + object);
		logger.error(builder.toString(), e);
		logger.error("------------------------end--------------------");
	}
	
	/**
	 * 输出url,params,ip,exception position,msg,异常堆信息
	 * @param object
	 * @param e
	 * @param request
	 */
	public  static void error(Object object, Throwable e  , HttpServletRequest request) 
	{
		if(request == null)
		{
			logger.error(object , e);
			return ;
		}
		
		Map<String , String[]> map = request.getParameterMap();
		Iterator<Entry<String , String[]>> iterator = map.entrySet().iterator();
		StringBuilder str = new StringBuilder();
		while(iterator.hasNext())
		{
			Entry<String , String[]> entry = iterator.next();
			str.append(entry.getKey());
			str.append(Arrays.toString(entry.getValue()));
		}
		String path =  request.getRequestURI();
		String ip = RequestUtil.getIpAddr(request);
		
		StringBuilder builder = new StringBuilder();
		builder.append("\n\r\n\r");
		builder.append("-----------------------error---------------------");
		builder.append("\nurl               : ");
		builder.append(path);
		builder.append("\nip                : ");
		builder.append(ip);
		builder.append("\nparams            : ");
		builder.append(str);
		builder.append("\nexception position: ");
		builder.append(Thread.currentThread().getStackTrace()[2].toString());
		builder.append("\nmsg               : ");
		builder.append(Thread.currentThread().getStackTrace()[2].toString() + " : " + object);
		logger.error(builder.toString(), e);
		logger.error("------------------------end-----------------------");
	}

}
